home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / misc / vuzkern.gz / vuzkern
Text File  |  1995-09-18  |  3KB  |  94 lines

  1. #!/usr/local/bin/perl
  2. #
  3. # vuzkern
  4. #
  5. # Read compressed kernels and extract their uncompressed binary image 
  6. # (or info, version str from it) to the standard output. 
  7. # Use it to see into your compressed kernels. I build too many for different
  8. # machines and forget versions and what they include 
  9. #
  10. # This is a quick hack: Rather than trying to follow internal structures
  11. # (which change) to spot the point where vmlinux-compressed starts, 
  12. # we skip some blocks and search for the gzip-deflated magic.
  13. # This is where the kernel starts (hopefully :).
  14. #
  15. # Copyright (c) 1995 Angelo Haritsis <ah@doc.ic.ac.uk>
  16. #
  17. # DISTRIBUTE FREELY, PROVIDED THE ABOVE COPYRIGHT IS KEPT. NO WARRANTIES.
  18. #
  19. # $Header: /usr/local/bin/RCS/vuzkern,v 1.1 1995/08/22 12:03:12 ah Exp ah $
  20. #
  21.  
  22. $GzipMagic = "\x1f\x8b\x8";    # deflate
  23. $SkipBlocks=25;            # kernel compressed bin is not there 
  24. $VersionReadBytes = 50*1024;    # bytes2read when seeking for version str
  25. $ZflagOffset=498;        # where kernel flags start
  26.  
  27. $progname = "vuzkern";
  28. $usage = "$progname v1.0: View inside a linux bootable zkernel
  29. usage: $progname [-v | -a | -r] vmlinuz ...
  30. Options:
  31.  -v    show only the kernel version string  (default)
  32.  -a    raw cat of kernel piped to 'strings -8'
  33.  -r    raw cat of kernel binary
  34.  
  35. Examples:
  36.  See the version of all your zkernels: $progname /vm* /dev/fd0
  37.  Browse ascii strings in a kernel: $progname -a vmlinuz | less
  38.  
  39. ";
  40.  
  41. $#ARGV >= 0 || die $usage;
  42.  
  43.  
  44. $check = 1;    # default = kernel version
  45.  
  46. # process command line options
  47. while ($_ = $ARGV[0], /^-/) {
  48.     shift;
  49.     last if /^--$/;
  50.     /-h|-\?/ && die $usage;
  51.     /-v/ && ($check = 1, next);
  52.     /-a/ && ($check = 2, next);
  53.     /-r/ && ($check = 3, next);
  54.     die "$progname: illegal option\n\n$usage";
  55. }
  56.  
  57. foreach $f (@ARGV) {
  58.     print STDERR ">> Kernel: [$f]\n";
  59.     open(ZKERN, "$f") || die "Cannot open $f\n";
  60.     if ($check != 3) {        # show all kernel flags also
  61.         seek(ZKERN, $ZflagOffset, 0);
  62.         read(ZKERN, $ZFlags, 14);
  63.         #syswrite(STDOUT, $ZFlags, 14); exit;
  64.         ($f_root, $f_sysize, $f_swapdev, $f_ram, $f_vid, $f_rootdev, $f_boot) =
  65.             unpack("S7", $ZFlags);
  66.  
  67.         $| = 1;
  68.         printf "rflag=%d, rootdev=%04X, swapdev=%04X, ram=%d, vid=%s, size=%d\n",
  69.         $f_root, $f_rootdev, $f_swapdev, $f_ram, $f_vid, $f_sysize*16;
  70.     }
  71.     seek(ZKERN, $SkipBlocks*512, 0);
  72.     # slurp the rest in (less than 500k)
  73.     $slurp_bytes = 500*1024;
  74.     # read less if we need only version
  75.     $check == 1 && ( $slurp_bytes = $VersionReadBytes  + 10*1024);
  76.     $num = read(ZKERN, $Buffer, $slurp_bytes);
  77.     # search for the gzip magic
  78.     $offset = index($Buffer, $GzipMagic);
  79.     # prepare popen  cmd
  80.     $gzip="gzip -dc 2>/dev/null";
  81.     $cmd = "$gzip | strings -8|grep \"Linux ver\"" if ($check == 1);
  82.     $cmd = "$gzip | strings -8" if ($check == 2);
  83.     $cmd = "$gzip" if ($check == 3);
  84.  
  85.     #print substr($Buffer, $offset); exit;
  86.     close(2);
  87.     open (KERN, "|$cmd") || die "Cannot open pipe\n";
  88.     ($check == 1) && print KERN substr($Buffer, $offset, $VersionReadBytes);
  89.     ($check != 1) && print KERN substr($Buffer, $offset);
  90.  
  91.     close KERN;
  92.     close ZKERN;
  93. }
  94.